package com.nhii.appframe.mvp.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jess.arms.base.BaseNoTitleActivity;
import com.jess.arms.di.component.AppComponent;
import com.nhii.appframe.di.component.DaggerMainComponent;
import com.nhii.appframe.mvp.contract.MainContract;
import com.nhii.appframe.mvp.presenter.MainPresenter;
import com.nhii.base.common.baseAdapter.MyBaseViewPageAdapter;
import com.nhii.base.common.entity.TabEntity;
import com.nhii.base.common.routerBean.RouterHub;
import com.nhii.mc.R;


import java.util.ArrayList;

import butterknife.BindView;

/**
 * ================================================
 * author : Alex
 * e-mail : xys9786@outlook.com
 * version: 1.0
 * Description:
 * <p>      项目的主页面 承载fragment
 * Created by MVPArmsTemplate on 04/27/2020 19:11
 * ================================================
 */
@Route(path = RouterHub.APP_MAINACTIVITY)
public class MainActivity extends BaseNoTitleActivity<MainPresenter> implements MainContract.View {

    private String[] mTitles = {"首页", "检查", "班级", "我的"};

    private int[] mIconUnselectIds = {
            R.mipmap.app_image_false_home,
            R.mipmap.app_image_false_inspect,
            R.mipmap.app_image_false_classgrade,
            R.mipmap.app_image_false_mine};

    private int[] mIconSelectIds = {
            R.mipmap.app_image_true_home,
            R.mipmap.app_image_true_inspect,
            R.mipmap.app_image_true_classgrade,
            R.mipmap.app_image_true_mine};

    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @BindView(R.id.ctl)
    CommonTabLayout mCommonTabLayout;

    @BindView(R.id.vp)
    ViewPager mViewPager;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    protected void onNetReload(View v) {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        showContent();
        //初始化页面
        initFragment();
        //设置适配器
        mViewPager.setAdapter(new MyBaseViewPageAdapter(getSupportFragmentManager(), mFragments, mTitles));
        //设置适配器缓存
        mViewPager.setOffscreenPageLimit(4);
        //初始化tab
        initTab();
    }

    /**
     * //初始化页面
     */
    private void initFragment() {

        Fragment homeFragment = (Fragment) ARouter.getInstance().build(RouterHub.TEST_TESTFRAGMENT).navigation();
        Fragment inspectFragment = (Fragment) ARouter.getInstance().build(RouterHub.TEST_TESTFRAGMENT).navigation();
        Fragment cgFragment = (Fragment) ARouter.getInstance().build(RouterHub.TEST_TESTFRAGMENT).navigation();
        Fragment mineFragment = (Fragment) ARouter.getInstance().build(RouterHub.TEST_TESTFRAGMENT).navigation();

        mFragments.add(homeFragment);
        mFragments.add(inspectFragment);
        mFragments.add(cgFragment);
        mFragments.add(mineFragment);

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
    }

    /**
     * 初始化tab
     */
    private void initTab() {
        mCommonTabLayout.setTabData(mTabEntities);

        mCommonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }
            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                mCommonTabLayout.setCurrentTab(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mCommonTabLayout.setCurrentTab(0);
    }


    /**
     * 设置热启动
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
